package com.tal.order.controller;

import com.tal.order.dto.Stock;
import com.tal.order.dto.User;
import com.tal.order.service.feignclient.UserServiceFeignClient;
import com.tal.order.service.feignclient.WarehouseServiceFeignClient;
import feign.Client;
import feign.Contract;
import feign.Feign;
import feign.codec.Decoder;
import feign.codec.Encoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClientsConfiguration;
import org.springframework.context.annotation.Import;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


@RestController
@Import(FeignClientsConfiguration.class)
public class OrderController {

    @Resource
    private WarehouseServiceFeignClient warehouseServiceFeignClient;

    @Autowired
    private Decoder decoder;

    @Autowired
    private Encoder encoder;

    @Autowired
    private Contract contract;

    @Autowired
    private Client client;

    /**
     * 创建订单业务逻辑
     *
     * @param skuId         商品类别编号
     * @param salesQuantity 销售数量
     * @return
     */
    @GetMapping("/createOrder")
    public Map createOrder(Long skuId, Long salesQuantity) {
        Map result = new LinkedHashMap();
        //查询商品库存，像调用本地方法一样完成业务逻辑。
        Stock stock = warehouseServiceFeignClient.getStock(skuId);
        System.out.println(stock);
        if (salesQuantity <= stock.getQuantity()) {
            //创建订单相关代码，此处省略
            //CODE=SUCCESS代表订单创建成功
            result.put("code", "SUCCESS");
            result.put("skuId", skuId);
            result.put("message", "订单创建成功");
        } else {
            //code=NOT_ENOUGN_STOCK代表库存不足
            result.put("code", "NOT_ENOUGH_STOCK");
            result.put("skuId", skuId);
            result.put("message", "商品库存数量不足");
        }
        return result;

    }

    @PostMapping("/returnOrder/{orderId}")
    public String returnOrder(@PathVariable("orderId") String orderId) {
        String msg = warehouseServiceFeignClient.updateStock(1101L);
        System.out.println(msg);
        return String.format("order %s returned success", orderId);
    }

    @PostMapping("/returnOrder")
    public String returnOrderDetail() {
        Stock stock = new Stock(1103L, "Apple iPhone 12 256GB 黑色", 2, "台");
        return warehouseServiceFeignClient.update(stock).toString();
    }

    @GetMapping("/shareOrders")
    public String shareOrders() {
        UserServiceFeignClient userServiceFeignClient = Feign.builder()
                // 默认注入的 client 需要进行 load balance, 如果通过 k8s service 进行负载均衡
                // 则不需要
//                .client(client)
                .encoder(encoder)
                .decoder(decoder)
                .contract(contract)
                .target(UserServiceFeignClient.class, "http://127.0.0.1:8011");
        List<User> userList = userServiceFeignClient.getFriends("123");
        return "share for users: " + userList.toString();
    }

}

